%% Produce Figure 3

addpath('functions');
addpath('data');

load('output\results_Figure3.mat')

v2struct(results);
load('recession.mat')

%% Colour Scheme

% Coloured Figures

% red = [1 0 0];
% blue = [0 0 1];
% bandscolor = 'b';

% Black and White Figures
% 
red = [0 0 0];
blue = [0 0 0];
bandscolor = 'k';

%% Produce Figure 3a
load('spf.mat');
datesforspf = dates1(3:end);
spfseries = spf(3:end-7,2);

endquarter = 63;
% 
indexaa = find(isfinite(spfseries(:,1)));
[trendRealTime_monthly]=dailyToLowerFrequency([datesSave trendRealTime],2,1);

f=figure;
plot(trendRealTime_monthly(:,1),trendRealTime_monthly(:,4),'color',blue);
hold on
plot(datesSave,meanRealTime(:,1),'--k','LineWidth',1)
plot(datesforspf(indexaa,1),spfseries(indexaa),'dk','MarkerSize',8,'LineWidth',1)

% bar(dates(1:end),4.99.*recession(1:size(dates,1)),'FaceColor',[0.9 0.9 0.9],'EdgeColor',[0.9 0.9 0.9],'LineWidth',0.1,'BarWidth',1)
plotConfidenceBandsBlue(trendRealTime_monthly(:,1),trendRealTime_monthly(:,2:end),bandscolor)  ;
hold on
plot(trendRealTime_monthly(:,1),trendRealTime_monthly(:,4),'color',blue,'LineWidth',2);
plot(datesSave,meanRealTime,'--k','LineWidth',1)


plot(datesforspf(indexaa,1),spfseries(indexaa),'dk','MarkerSize',8,'LineWidth',1)
hold off       
        
datesToPlot = datenum(2000:2:2015,1,31);
YAxisTicks = [0:1:5];
set(gca,'XTick',datesToPlot,'GridLineStyle',':','YTick',YAxisTicks)
datetick('x','yyyy','keepticks')
xmin=datenum(2000,1,01);
xmax=datenum(2015,07,31);
xlim([xmin,xmax])
ymin = 0;
ymax = 5;
ylim([ymin,ymax])
grid on
legend_h=legend('Real-time estimate of current long-run growth','Constant mean estimated in real time','Survey','Location','northoutside','Orientation','horizontal');
legend('boxoff')
legendPos=[0.400267094906627,0.90,0.211666664887443,0.136182595720040];
set(legend_h,'position',legendPos)

figSize = [10 7];
set(f, 'PaperUnits', 'inches');
set(f, 'Units','inches');
set(f, 'PaperSize', figSize);
set(f, 'PaperPositionMode', 'auto');
set(f, 'Position', [0 0 figSize(1) figSize(2)])
set(gca, 'FontName', 'Times-Roman');
set(gca, 'FontSize', 14);
set(gca, 'Layer','top');
% set(gca,'OuterPosition',[-0.12 -.06 1.2 1.1]);

ti = get(gca,'TightInset');
set(gca,'Position',[1.1*ti(1) 0.99*ti(2) 0.99*(1-ti(3)-ti(1)) 0.95*(1-ti(4)-ti(2))]);

fname = strcat('output\Figure3a.pdf');
print('-dpdf', f, fname);

%% Figure 3b

p = 2;


trendVintages(trendVintages==0) = nan;
trendVintages_lastObs = nan*trendVintages;

B = ~isnan(trendVintages);
Indices = arrayfun(@(x) find(B(:, x), 1, 'last'), 1:size(trendVintages, 2));
linearInd = sub2ind(size(trendVintages),Indices,1:size(trendVintages, 2));
trendVintages_lastObs(linearInd) = trendVintages(linearInd);


f=figure;
vintagesToPlot = (1:round(365.25*1):5834)';
ax = gca;
ax.ColorOrderIndex = 1;
plot(dates(3:819+2,1),trendVintages(:,vintagesToPlot(1)),'--','color',[0.5 0.5 0.5],'LineWidth',1)

hold on

plot(dates(p+1:819+p,1),trendVintages_lastObs(:,vintagesToPlot(1)),'o','MarkerFaceColor',blue,'MarkerSize',4,'color',blue,'LineWidth',1)
plot(dates(p+1:819+p,1),trendVintages(:,end),'color',red,'LineWidth',2.5)



plot(dates(p+1:819+p,1),trendVintages(:,end),'color',red,'LineWidth',2.5)

% bar(dates(1:end),4.99.*recession(1:size(dates,1)),'FaceColor',[0.9 0.9 0.9],'EdgeColor',[0.9 0.9 0.9],'LineWidth',0.1,'BarWidth',1)
ax = gca;
ax.ColorOrderIndex = 1;
plot(dates(p+1:819+p,1),trendVintages(:,vintagesToPlot),'--','color',[0.5 0.5 0.5],'LineWidth',1)
plot(dates(p+1:819+p,1),trendVintages(:,end),'color',red,'LineWidth',2.5)
plot(dates(p+1:819+p,1),trendVintages_lastObs(:,vintagesToPlot),'o','MarkerFaceColor',blue,'MarkerSize',4,'color',blue,'LineWidth',1)

hold off       
        
datesToPlot = datenum(2000:2:2015,1,31);
YAxisTicks = [0:1:5];
set(gca,'XTick',datesToPlot,'GridLineStyle',':','YTick',YAxisTicks)
datetick('x','yyyy','keepticks')
xmin=datenum(2000,1,31);
xmax=datenum(2015,7,31);
xlim([xmin,xmax])
ymin = 0;
ymax = 5;
ylim([ymin,ymax])
grid on
legend_h=legend('Vintages of long-run growth estimates','Real-time estimate of current long-run growth','Latest estimate','Location','northoutside','Orientation','horizontal');
legend('boxoff')
legendPos=[0.400267094906627,0.90,0.211666664887443,0.136182595720040];
set(legend_h,'position',legendPos)

figSize = [10 7];
set(f, 'PaperUnits', 'inches');
set(f, 'Units','inches');
set(f, 'PaperSize', figSize);
set(f, 'PaperPositionMode', 'auto');
set(f, 'Position', [0 0 figSize(1) figSize(2)])
set(gca, 'FontName', 'Times-Roman');
set(gca, 'FontSize', 14);
set(gca, 'Layer','top');
% set(gca,'OuterPosition',[-0.12 -.06 1.2 1.1]);

ti = get(gca,'TightInset');
set(gca,'Position',[1.1*ti(1) 0.99*ti(2) 0.99*(1-ti(3)-ti(1)) 0.95*(1-ti(4)-ti(2))]);

fname = strcat('output\Figure3b.pdf');
print('-dpdf', f, fname);